﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.Net;
using OpenNETCF.Net.NetworkInformation;
using System.Web.Services;
using System.Xml;
using PDAMain.DAL;
using System.Text.RegularExpressions;
using PDAMain.Entity;

namespace PDAMain
{
    public partial class Config : Form
    {        
        public Config()
        {
            InitializeComponent();
        }

        private void Config_Load(object sender, EventArgs e)
        {
            if (!ConfigHelper.CheckConfigFile())
            {
                ConfigHelper.InitConfigFile();
            }

            txtUpdateTime.ForeColor.Equals("Gray");
            macNameBindData();
            txtWebService.Text = ConfigHelper.GetConfigValue("Webservice");
            int time = int.Parse(ConfigHelper.GetConfigValue("Time")) / 60 / 1000;
            txtUpdateTime.Text = time.ToString();
            for (int index = 0; index < macName.Items.Count; index++)
            {
                if (macName.Items[index].ToString().Trim() == ConfigHelper.GetConfigValue("Mac"))
                {
                    macName.SelectedIndex = index;
                }
            }
            txtPicurl.Text = ConfigHelper.GetConfigValue("PicUrl");
            txtPort.Text = ConfigHelper.GetConfigValue("Port");
            txtUserName.Text = ConfigHelper.GetConfigValue("User");
            txtPwd.Text = GetReturnXmlString(ConfigHelper.GetConfigValue("Pwd"));
        }
        private void macNameBindData()
        {

            macName.Items.Clear();
            macName.Items.Insert(0, "--请选择--");
            int count = 1;
            foreach (INetworkInterface currentInterface in NetworkInterface.GetAllNetworkInterfaces())
            {
                this.macName.Items.Insert(count, currentInterface.Name.ToString());
                count++;
            }
            macName.SelectedIndex = 0;
        }
        

        private void confirm_Click(object sender, EventArgs e)
        {
            //更新webService
           string webServiceAddress = txtWebService.Text.Trim();//webService地址
           //webServiceAddress = "http://10.134.97.80:8080/pda_web/services/pdaService?wsdl";
           int updateTime;//timer的时间间隔
           string macAddressName=string.Empty; //网卡地址的名称

           pdaWebReference.PdaService reference = new PDAMain.pdaWebReference.PdaService();
           try
           {
               reference.Url = webServiceAddress;
           }
           catch (Exception ex)
           {
               MessageBox.Show("请输入正确的地址！");
               return;
           }

           if (txtUpdateTime.Text.Trim() == "(默认为5分钟)")
           {
               updateTime = 5 * 60 * 1000;
           }
           else if (txtUpdateTime.Text.Trim() == "")
           {
               MessageBox.Show("请输入同步时间！");
               return;
           }
           else if (txtUpdateTime.Text.Trim() == "0")
           {
               MessageBox.Show("同步时间间隔不能为0！");
               return;
           }
           else
           {
               updateTime = int.Parse(txtUpdateTime.Text.Trim()) * 60 * 1000;
           }
           if (macName.SelectedIndex == 0)
           {
               MessageBox.Show("请选择设备网卡名称！");
               return;
           }
           else
           {
               macAddressName = macName.SelectedItem.ToString().Trim();
           }
           if (txtPicurl.Text.Trim() == "")
           {
               MessageBox.Show("请输入上传图片IP地址");
               return;
           }
           Regex reg = new Regex(@"\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}");
           if (!reg.IsMatch(txtPicurl.Text.Trim()))
           {
               MessageBox.Show("请输入正确的上传图片IP地址");
               return;
           }
           if (txtPort.Text.Trim() == "")
           {
               MessageBox.Show("请输入端口号");
               return;
           }
           if (!DataHelper.CustomerWorkDao.verifyData(txtPort.Text.Trim(), "int"))
           {
               MessageBox.Show("请输入正确的端口号");
               return;
           }
           if (txtUserName.Text.Trim() == "")
           {
               MessageBox.Show("请输入用户名");
               return;
           }
           if (txtPwd.Text.Trim() == "")
           {
               MessageBox.Show("请输入密码");
               return;
           }
           XmlDocument xml = new XmlDocument();
           xml.Load(ConfigHelper.ConfigPath);
           XmlNode xn = xml.SelectSingleNode("Settings");
           XmlNodeList xnl = xn.ChildNodes;
           foreach (XmlNode xnf in xnl)
           {
               if (xnf.Name == "Webservice")
               {
                   xnf.InnerText = webServiceAddress;
               }
               else if (xnf.Name == "Time")
               {
                   xnf.InnerText = updateTime.ToString();
               }
               else if (xnf.Name == "Mac")
               {
                   xnf.InnerText = macAddressName;
               }
               else if (xnf.Name == "PicUrl")
               {
                   xnf.InnerText = txtPicurl.Text.Trim();
               }
               else if (xnf.Name == "Port")
               {
                   xnf.InnerText = txtPort.Text.Trim();
               }
               else if (xnf.Name == "User")
               {
                   xnf.InnerText = GetXmlString(txtUserName.Text.Trim());
               }
               else if (xnf.Name == "Pwd")
               {
                   xnf.InnerText = GetXmlString(txtPwd.Text.Trim());
               }
           }
           xml.Save(ConfigHelper.ConfigPath);
           MessageBox.Show("保存成功！");
           BackForm();
        }

        //处理XML中的特殊定义符
        private string GetXmlString(string value)
        {
            string result="";
            char[] Str = value.ToCharArray();
            for (int i = 0; i < Str.Length; i++)   //XML文件有5个特殊字符串需特别处理
            {
                string s = Str[i].ToString();
                switch (s)
                {
                    case "<":
                        {
                            s = "&lt;";
                            break;
                        }
                    case ">":
                        {
                            s = "&gt;";
                            break;
                        }
                    case "&":
                        {
                            s = "&amp;";
                            break;
                        }
                    case "'":
                        {
                            s = "&apos;";
                            break;
                        }
                    case "\"":
                        {
                            s = "&quot;";
                            break;
                        }
                }
                result += s;
            }
            return result;
        }

        //处理XML中的特殊定义符
        private string GetReturnXmlString(string value)
        {
            return value.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&amp;", "&").Replace("&apos;", "'").Replace("&quot;", "\"");
        }

        private void txtUpdateTime_GotFocus(object sender, EventArgs e) 
        {
            txtUpdateTime.Text = "";
            txtUpdateTime.ForeColor.Equals("black");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            BackForm();
        }

        private void BackForm()
        {
            if (Identity.dutyPerson != "")
            {
                FuntionView formShow = new FuntionView();
                formShow.Show();
                this.Close();
            }
            else
            {
                this.Close();
            }
        }
    }
}